package cn.org.intelli.lims.o2oa.action;

import cn.org.intelli.lims.o2oa.util.Crypto;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import xyz.erupt.annotation.sub_erupt.Tpl;
import xyz.erupt.tpl.annotation.EruptTpl;
import xyz.erupt.tpl.annotation.TplAction;
import xyz.erupt.upms.service.EruptUserService;

@Service
@EruptTpl(engine = Tpl.Engine.FreeMarker)
public class O2oaAction {

    @Resource
    private EruptUserService eruptUserService;
    @Value("${lims-plus.o2oa.sso-client-name}")
    private String ssoClientName;
    @Value("${lims-plus.o2oa.sso-key}")
    private String ssoKey;
    @Value("${lims-plus.o2oa.sso-url}")
    private String ssoUrl;
    @Value("${lims-plus.o2oa.sso-redirect}")
    private String ssoRedirect;

    @SneakyThrows
    @TplAction("sso.ftl")
    public Map<String, Object> o2oaSso() {
        String sso_client_name = ssoClientName;
        String sso_key = ssoKey;
        String sso_url = ssoUrl;
        String login_uid = eruptUserService.getCurrentEruptUser().getAccount();
        long time = System.currentTimeMillis();
        String xtoken = null;
        xtoken = Crypto.encrypt(login_uid + "#" + time, sso_key);
        Map<String, Object> map = new HashMap<>();
        map.put("sso_url_with_token", sso_url + "?client=" + sso_client_name + "&xtoken=" + xtoken + "&redirect=" + ssoRedirect);
        return map;
    }
}
